package jwt

import "github.com/dgrijalva/jwt-go"

type JWT struct {
	TokenId string
	jwt.StandardClaims
}

var secret = []byte("123456")
var issuer = "ydc"

func GenerateToken(id string) string {
	jwtData := JWT{
		TokenId: id,
		StandardClaims: jwt.StandardClaims{
			Issuer: issuer,
		},
	}
	token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwtData)
	signedToken, err := token.SignedString(secret)
	if err != nil {
		panic(err)
	}

	return signedToken
}
func ParseToken(token string) (*JWT, error) {
	mc := new(JWT)
	_, err := jwt.ParseWithClaims(token, mc, func(token *jwt.Token) (interface{}, error) {
		return secret, nil
	})
	if err != nil {
		return nil, err
	}
	return mc, nil
}
